﻿package Classes.Utils
{
	import flash.display.*;
	import Classes.Elements.Aircraft;
	
	public class Moving{		
		static public function pointTo(mc:MovieClip,target:MovieClip,omega:Number=0):void{
			var dx:Number = mc.x - target.x;
   			var dy:Number = mc.y - target.y;
			//target与y轴的夹角
    		var angle:Number = (270 + Math.atan2(dy, dx)*180/Math.PI)%360;
			//target与mc的夹角
			var crtangle:Number = (angle - mc.rotation + 360)%360;
			//判断mc旋转方向
			var dir:int = crtangle<=180?1:-1;
			if(omega)
				mc.rotation = (crtangle<180 && crtangle>omega || crtangle>180 && 360-crtangle>omega)? (mc.rotation+omega*dir) : angle;
			else
				mc.rotation = angle;
		}
		
		static public function moveDirectly(sprite:MovieClip,vspeed:Number):void{
			sprite.y += vspeed;			
		}
		
		static public function moveBeeline(mc:MovieClip,speed:Number,angle:Number):void{
			if(angle!=0){
				mc.rotation = -angle;
				mc.x += speed * Math.sin(angle/180*Math.PI);
				mc.y += speed * Math.cos(angle/180*Math.PI);
			}
			else{
				mc.y += speed;
			}
		}		
		
		public static function Track(target:MovieClip,mc:MovieClip,speed:Number,omega:Number=0,dir:int=1):void{
			if(!target){
				Moving.moveDirectly(mc,dir*speed/2);
				mc.rotation = dir>0?180:0;
			}
			else{
				var dx:Number = mc.x - target.x;
   				var dy:Number = mc.y - target.y;
				//target与y轴的夹角
    			var angle:Number = (270 + Math.atan2(dy, dx)*180/Math.PI)%360;
				//target与mc的夹角
				var crtangle:Number = (angle - mc.rotation + 360)%360;
				//判断mc旋转方向
				var dir:int = crtangle<=180?1:-1;
				if(omega)
					mc.rotation = (crtangle<180 && crtangle>omega || crtangle>180 && 360-crtangle>omega)? (mc.rotation+omega*dir) : angle;
				else
					mc.rotation = angle;
				}
				mc.x += speed*Math.sin(mc.rotation*Math.PI/180);	
				mc.y -= speed*Math.cos(mc.rotation*Math.PI/180);				
		}
		static public function moveSlanting(mc:MovieClip,vspeed:Number,hspeed:Number,startSlanting:Number):void{
			mc.y += vspeed;
			if(mc.y>startSlanting)
				mc.x += hspeed;			
		}		
		
		static public function moveCircle(mc:MovieClip,speed:Number,radius:Number,angle:Number,centerx:Number,centery:Number):int{			
			mc.x = radius*Math.sin(angle*Math.PI/180) + centerx;
			mc.y = radius*Math.cos(angle*Math.PI/180)+ centery;
			var an:Number = Math.abs(angle%360);
			if(an<60 || an>300){
				return 1;
			}
			else if(an>120 && an<240){
				return -1;	
			}
			else {				
				return 0;
			}			
		}		
	}
}